//
//  MessageCell.m
//  ChatInterface_QQ
//
//  Created by admin on 2019/9/1.
//  Copyright © 2019 猫爷爷的酱醋茶. All rights reserved.
//

#import "MessageCell.h"
#import "MessageFrame.h"
#import "Message.h"

@interface MessageCell()

@property(nonatomic,weak)UILabel *timeLabel;
@property(nonatomic,weak)UIImageView *iconImageView;
@property(nonatomic,weak)UIButton *messageBtn;

@end


@implementation MessageCell

#pragma mark - 创建cell的类方法
+ (instancetype)messgeFrameWithUItableView:(UITableView *)tableView
{
    static NSString *ID = @"QQ_ID";
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil)
    {
        cell = [[MessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

#pragma mark - 重写单元格初始化方法——initWithStyle方法

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //创建子控件
        //显示时间的Label
        UILabel *timeLabel = [[UILabel alloc]init];
        //设置文字的大小
        timeLabel.font = [UIFont systemFontOfSize:12];
        //设置文字的对齐方式
        timeLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:timeLabel];
        self.timeLabel = timeLabel;
        
        //显示头像的UIImageView
        UIImageView *iconImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:iconImageView];
        self.iconImageView = iconImageView;
        
        //显示正文的按钮
        UIButton *messageBtn = [[UIButton alloc]init];
        [self.contentView addSubview:messageBtn];
        //设置正文字体大小
        messageBtn.titleLabel.font = textFont;
        //设置正文文字颜色
        [messageBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //设置文字自动换行
        messageBtn.titleLabel.numberOfLines = 0;
        self.messageBtn = messageBtn;
    }
    
    //设置单元格的背景色为clearColor
    self.backgroundColor = [UIColor clearColor];
    
    return  self;
}

#pragma mark - 重写frame的方法
- (void)setMessageFrame:(MessageFrame *)messageFrame
{
    _messageFrame = messageFrame;
    
    //获取数据模型
    Message *message = messageFrame.message;
    //设置时间Label的数据和frame
    self.timeLabel.text = message.time;
    self.timeLabel.frame = messageFrame.timeFrame;
    //如果和上一个数据模型的时间一致，隐藏时间label
    self.timeLabel.hidden = message.hideTime;
    
    //设置头像的数据和frame
    //根据消息类型，判断使用哪张头像
    NSString *iconImage = message.type == MessageTypeMe ? @"me" : @"other";
    self.iconImageView.image = [UIImage imageNamed:iconImage];
    self.iconImageView.frame = messageFrame.iconFrame;
    
    //设置正文按钮的数据和frame
    [self.messageBtn setTitle:message.text forState:UIControlStateNormal];
    self.messageBtn.frame = messageFrame.messageFrame;
    
    //设置正文的背景图片
    NSString *imageNorName,*imageHighlitName;
    if (message.type == MessageTypeMe)
    {
        //自发的消息
        imageNorName = @"chat_send_nor";
        imageHighlitName = @"chat_send_press_pic";
    
        [self.messageBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        imageNorName = @"chat_recive_nor";
        imageHighlitName = @"chat_recive_press_pic";
        
        [self.messageBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    UIImage *imageNor = [UIImage imageNamed:imageNorName];
    UIImage *imageHighlit = [UIImage imageNamed:imageHighlitName];
    
    //用平铺的方式设置图片
    imageNor  = [imageNor stretchableImageWithLeftCapWidth:imageNor.size.width *0.5 topCapHeight:imageNor.size.height *0.5];
    imageHighlit = [imageHighlit stretchableImageWithLeftCapWidth:imageHighlit.size.width *0.5 topCapHeight:imageHighlit.size.height *0.5];
    
    //设置正文的内间距
    self.messageBtn.contentEdgeInsets = UIEdgeInsetsMake(15, 20, 15, 20);
    
    [self.messageBtn setBackgroundImage:imageNor forState:UIControlStateNormal];
    [self.messageBtn setBackgroundImage:imageHighlit forState:UIControlStateHighlighted];
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
